/**
 * Created by vincent on 2017/9/29.
 */
moduleDynamicHtml.controller('DynamicHtmlQuery', function (LanguageService, $scope, $log, $state, $timeout, DialogService, LoadingService, ConstantService, DynamicHtmlResource) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('网页管理'));
        $scope.pagination = new Pagination();
        $scope.pagination.setPageClickFn($scope.queryHtmlList);
        $scope.queryHtmlList($scope.pagination.currentPageNo);
    });

    $scope.queryPara = {};
    $scope.queryHtmlList = function (pageNo) {
        //LoadingService.show();
        DynamicHtmlResource.queryList({pageNo: pageNo - 1, pageSize: 10}, $scope.queryPara, function (htmlResult) {
            $log.debug("H5项目搜索结果", htmlResult);
            $scope.htmlResult = htmlResult;
            $scope.pagination.setPage(htmlResult, pageNo);
            //LoadingService.hide();
        });
    };

    $scope.addHtml = function () {
        DialogService.open("addHtml");
    };

    $scope.editHtml = function (html) {
        $scope.currentHtml = html;
        //LoadingService.show();
        DynamicHtmlResource.findById({dynamicHtmlId: $scope.currentHtml.dynamicHtmlId}, function (htmlResult) {
            $log.debug("H5项目", htmlResult);
            $scope.editingHtml = angular.copy(htmlResult);
            DialogService.open("editHtml", $scope);
            //LoadingService.hide();
        });
    };

    //$scope.deleteHtml = function(html){
    //    Alert.confirm("是否确认删除该项目？",function(){
    //        //LoadingService.show();
    //        DynamicHtmlResource.delete({id:html.dynamicHtmlId},function(){
    //            $scope.queryHtmlList($scope.pagination.currentPageNo);
    //            $timeout(function(){
    //                //LoadingService.hide();
    //                Alert.success("删除成功！");
    //            },300);
    //        },function(){
    //            //LoadingService.hide();
    //        });
    //    });
    //};

    $scope.reloadHtml = function (html) {
        Alert.confirm("是否确认加载该项目？", function () {
            //LoadingService.show();
            DynamicHtmlResource.reload({id: html.dynamicHtmlId}, function () {
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success();
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.reloadAll = function () {
        Alert.confirm("是否确认加载该项目？", function () {
            //LoadingService.show();
            DynamicHtmlResource.reloadAll(function () {
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success();
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.refresh = function () {
        $scope.queryPara = {};
    };
});